/*****************************************************************************************
 * FILE NAME: test_chaodian_can_L                        COPYRIGHT (c) TX 2025
 * REVISION:  1.0.0                                     All Rights Reserved
 * DESCRIPTION: 超电CAN协议模块单元测试实现文件
 * =======================================================================================
 * UPDATE HISTORY：
 * REV      AUTHOR      DATE
 * v1.0.0   fangangchuan  2025.10.15
 * DESCRIPTION OF CHANGE: 实现超电CAN协议模块的单元测试
 ****************************************************************************************/

//----------------------------------------------------------------------------------------
// @Project Includes
#include "test_chaodian_can_L.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module

/* 测试统计变量 */
uint32_t test_passed_count = 0;
uint32_t test_failed_count = 0;
uint32_t test_start_time = 0;
uint32_t test_suite_start_time = 0;

/* 模拟CAN全局变量 */
stc_can_t g_stccan = {0};

/* 应答帧检测辅助函数 */
void test_verify_response_frame(uint8_t expected_func_code, uint8_t expected_reg_count_high, 
                               uint8_t expected_reg_count_low, uint8_t expected_frame_index,
                               const char* test_name)
{
    uint8_t tx_count = mock_can_get_tx_count();
    TEST_ASSERT_EQUAL_UINT8(1, tx_count);  // 应该发送1个应答帧
    
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    
    // 验证功能码
    TEST_ASSERT_EQUAL_UINT8(expected_func_code, msg->Data[0]);
    
    // 验证寄存器数量
    TEST_ASSERT_EQUAL_UINT8(expected_reg_count_high, msg->Data[1]);
    TEST_ASSERT_EQUAL_UINT8(expected_reg_count_low, msg->Data[2]);
    
    // 验证帧索引
    TEST_ASSERT_EQUAL_UINT8(expected_frame_index, msg->Data[3]);
    
    printf("  ✓ %s: Response frame verified (Func=0x%02X, Regs=%d, Frame=%d)\n", 
           test_name, expected_func_code, (expected_reg_count_high << 8) | expected_reg_count_low, expected_frame_index);
}

void test_verify_error_response_frame(uint8_t expected_error_func_code, uint8_t expected_error_code,
                                    const char* test_name)
{
    uint8_t tx_count = mock_can_get_tx_count();
    TEST_ASSERT_EQUAL_UINT8(1, tx_count);  // 应该发送1个错误应答帧
    
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    
    // 验证错误功能码
    TEST_ASSERT_EQUAL_UINT8(expected_error_func_code, msg->Data[0]);
    
    // 验证错误码
    TEST_ASSERT_EQUAL_UINT8(expected_error_code, msg->Data[4]);
    
    printf("  ✓ %s: Error response frame verified (ErrorFunc=0x%02X, ErrorCode=0x%02X)\n", 
           test_name, expected_error_func_code, expected_error_code);
}

/* 模拟全局变量 */
uint16_t g_test_speed_value = 0;
uint16_t g_test_gps_speed_limit_value = 0;
uint8_t g_test_init_finish_flag = 0;
char g_test_iccid_sn[20] = "1234567890123456789";
char g_test_imei_sn[16] = "123456789012345";
uint8_t g_test_ble_mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};

/* 模拟原始模块的全局变量结构体 */
typedef struct {
    struct {
        uint16_t u16designVol;
        char ciccidSn[20];
        char cimeiSn[16];
    } stcConfig;
} stc_parm_t;

typedef struct {
    uint8_t binitFinishFlg;
    uint8_t u8bleMac[6];
} stc_var_t;

stc_parm_t g_stcParm = {
    .stcConfig = {
        .u16designVol = 0x1234,
        .ciccidSn = "1234567890123456789",
        .cimeiSn = "123456789012345"
    }
};

stc_var_t g_stcVar = {
    .binitFinishFlg = 1,
    .u8bleMac = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
};

/* 模拟系统时钟 */
static uint32_t mock_systick_tick = 0;

//----------------------------------------------------------------------------------------
// @Local Variables

//----------------------------------------------------------------------------------------
// 测试框架函数实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_framework_init(void)
 * @Description: 初始化测试框架
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_framework_init(void)
{
    test_passed_count = 0;
    test_failed_count = 0;
    test_start_time = 0;
    test_suite_start_time = 0;
    mock_systick_tick = 0;
    
    // 重置CAN模拟
    mock_can_reset();
    
    // 初始化模拟数据
    test_setup_mock_data();
    
    printf("Test Framework Initialized\n");
}

/*
 * @Functions:   void test_framework_cleanup(void)
 * @Description: 清理测试框架
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_framework_cleanup(void)
{
    test_cleanup_mock_data();
    printf("Test Framework Cleaned Up\n");
}

/*
 * @Functions:   void test_framework_print_stats(void)
 * @Description: 打印测试统计信息
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_framework_print_stats(void)
{
    printf("\n=== Test Framework Statistics ===\n");
    printf("Total Tests: %d\n", test_passed_count + test_failed_count);
    printf("Passed: %d\n", test_passed_count);
    printf("Failed: %d\n", test_failed_count);
    printf("Success Rate: %.2f%%\n", 
           (float)test_passed_count * 100.0f / (test_passed_count + test_failed_count));
    printf("================================\n");
}

/*
 * @Functions:   stc_test_stats_t test_framework_get_stats(void)
 * @Description: 获取测试统计信息
 * @Parameters:  NONE
 * @Return:      测试统计信息
 */
stc_test_stats_t test_framework_get_stats(void)
{
    stc_test_stats_t stats;
    stats.total_tests = test_passed_count + test_failed_count;
    stats.passed_tests = test_passed_count;
    stats.failed_tests = test_failed_count;
    stats.total_time_ms = mock_drv_systick_getTick() - test_suite_start_time;
    return stats;
}

//----------------------------------------------------------------------------------------
// 模拟函数实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   uint32_t mock_drv_systick_getTick(void)
 * @Description: 模拟系统时钟获取
 * @Parameters:  NONE
 * @Return:      模拟的系统时钟值
 */
uint32_t mock_drv_systick_getTick(void)
{
    return mock_systick_tick;
}

/*
 * @Functions:   void mock_drv_systick_setTick(uint32_t tick)
 * @Description: 设置模拟系统时钟
 * @Parameters:  tick - 时钟值
 * @Return:      NONE
 */
void mock_drv_systick_setTick(uint32_t tick)
{
    mock_systick_tick = tick;
}

/*
 * @Functions:   void mock_can_reset(void)
 * @Description: 重置CAN模拟
 * @Parameters:  NONE
 * @Return:      NONE
 */
void mock_can_reset(void)
{
    memset(&g_stccan, 0, sizeof(stc_can_t));
}

/*
 * @Functions:   uint8_t mock_can_get_tx_count(void)
 * @Description: 获取CAN发送计数
 * @Parameters:  NONE
 * @Return:      CAN发送计数
 */
uint8_t mock_can_get_tx_count(void)
{
    return g_stccan.vu8canTxMQPushLen;
}

/*
 * @Functions:   stc_can_message_t* mock_can_get_tx_message(uint8_t index)
 * @Description: 获取CAN发送消息
 * @Parameters:  index - 消息索引
 * @Return:      CAN消息指针
 */
stc_can_message_t* mock_can_get_tx_message(uint8_t index)
{
    if (index < CAN_MAX_TX_SIZE)
    {
        return &g_stccan.canTxMessage[index];
    }
    return NULL;
}

//----------------------------------------------------------------------------------------
// 测试辅助函数实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_setup_mock_data(void)
 * @Description: 设置模拟数据
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_setup_mock_data(void)
{
    g_test_speed_value = 50;  // 50 m/s
    g_test_gps_speed_limit_value = 30;  // 30 m/s
    g_test_init_finish_flag = 1;
    
    strcpy(g_test_iccid_sn, "1234567890123456789");
    strcpy(g_test_imei_sn, "123456789012345");
    g_test_ble_mac[0] = 0xAA;
    g_test_ble_mac[1] = 0xBB;
    g_test_ble_mac[2] = 0xCC;
    g_test_ble_mac[3] = 0xDD;
    g_test_ble_mac[4] = 0xEE;
    g_test_ble_mac[5] = 0xFF;
}

/*
 * @Functions:   void test_cleanup_mock_data(void)
 * @Description: 清理模拟数据
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cleanup_mock_data(void)
{
    memset(&g_test_speed_value, 0, sizeof(g_test_speed_value));
    memset(&g_test_gps_speed_limit_value, 0, sizeof(g_test_gps_speed_limit_value));
    memset(&g_test_init_finish_flag, 0, sizeof(g_test_init_finish_flag));
    memset(g_test_iccid_sn, 0, sizeof(g_test_iccid_sn));
    memset(g_test_imei_sn, 0, sizeof(g_test_imei_sn));
    memset(g_test_ble_mac, 0, sizeof(g_test_ble_mac));
}

/*
 * @Functions:   void test_print_can_message(const stc_can_message_t* msg)
 * @Description: 打印CAN消息
 * @Parameters:  msg - CAN消息指针
 * @Return:      NONE
 */
void test_print_can_message(const stc_can_message_t* msg)
{
    if (msg == NULL) return;
    
    printf("CAN Message: ID=0x%08X, IDE=%d, RTR=%d, DLC=%d, Data=", 
           msg->ExtId, msg->IDE, msg->RTR, msg->DLC);
    
    for (int i = 0; i < msg->DLC; i++)
    {
        printf("%02X ", msg->Data[i]);
    }
    printf("\n");
}

/*
 * @Functions:   void test_print_hex_data(const uint8_t* data, uint8_t length)
 * @Description: 打印十六进制数据
 * @Parameters:  data - 数据指针
 *               length - 数据长度
 * @Return:      NONE
 */
void test_print_hex_data(const uint8_t* data, uint8_t length)
{
    if (data == NULL) return;
    
    printf("Data[%d]: ", length);
    for (int i = 0; i < length; i++)
    {
        printf("%02X ", data[i]);
    }
    printf("\n");
}

//----------------------------------------------------------------------------------------
// 基础工具函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_build_id(void)
 * @Description: 测试CAN ID构建函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_build_id(void)
{
    TEST_START();
    
    uint32_t can_id;
    
    // 测试正常情况
    can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0001, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
    TEST_ASSERT_EQUAL_UINT32(0x030001AB, can_id);
    
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x0002, CD_DEVICE_ADDR_CHARGER, CD_DEVICE_ADDR_BMS);
    TEST_ASSERT_EQUAL_UINT32(0x100002CB, can_id);
    
    // 测试边界情况
    can_id = cd_can_build_id(0xFF, 0xFFFF, 0x0F, 0x0F);
    TEST_ASSERT_EQUAL_UINT32(0xFFFFFFFF, can_id);
    
    can_id = cd_can_build_id(0x00, 0x0000, 0x00, 0x00);
    TEST_ASSERT_EQUAL_UINT32(0x00000000, can_id);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_parse_id(void)
 * @Description: 测试CAN ID解析函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_parse_id(void)
{
    TEST_START();
    
    uint8_t func_code, dst_addr, src_addr;
    uint16_t reg_addr;
    uint8_t result;
    
    // 测试正常情况
    result = cd_can_parse_id(0x030001AB, &func_code, &reg_addr, &dst_addr, &src_addr);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG, func_code);
    TEST_ASSERT_EQUAL_UINT16(0x0001, reg_addr);
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_HOST, dst_addr);
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_BMS, src_addr);
    
    result = cd_can_parse_id(0x100002CB, &func_code, &reg_addr, &dst_addr, &src_addr);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_WRITE_REG, func_code);
    TEST_ASSERT_EQUAL_UINT16(0x0002, reg_addr);
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_CHARGER, dst_addr);
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_BMS, src_addr);
    
    // 测试边界情况（使用合法地址）
    result = cd_can_parse_id(0xFFFFCBAB, &func_code, &reg_addr, &dst_addr, &src_addr);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(0xFF, func_code);
    TEST_ASSERT_EQUAL_UINT16(0xFFCB, reg_addr);
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_HOST, dst_addr);     // 0x0A
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_BMS, src_addr);      // 0x0B
    
    // 测试非法地址
    result = cd_can_parse_id(0x0300010D, &func_code, &reg_addr, &dst_addr, &src_addr);
    TEST_ASSERT_EQUAL_UINT8(0, result);  // 应该返回失败
    
    result = cd_can_parse_id(0x03000D01, &func_code, &reg_addr, &dst_addr, &src_addr);
    TEST_ASSERT_EQUAL_UINT8(0, result);  // 应该返回失败
    
    // 测试空指针（现在允许NULL参数）
    result = cd_can_parse_id(0x030001AB, NULL, NULL, &dst_addr, NULL);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_HOST, dst_addr);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_is_for_me(void)
 * @Description: 测试CAN消息目标检查函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_is_for_me(void)
{
    TEST_START();
    
    uint8_t result;
    
    // 测试目标地址匹配
    result = cd_can_is_for_me(0x030001AB, CD_DEVICE_ADDR_HOST);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    result = cd_can_is_for_me(0x100002CB, CD_DEVICE_ADDR_CHARGER);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试目标地址不匹配
    result = cd_can_is_for_me(0x030001AB, CD_DEVICE_ADDR_BMS);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    result = cd_can_is_for_me(0x100002CB, CD_DEVICE_ADDR_BMS);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试边界情况（使用合法地址）
    result = cd_can_is_for_me(0x000000AA, CD_DEVICE_ADDR_HOST);  // 0x0A
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    result = cd_can_is_for_me(0x000000BB, CD_DEVICE_ADDR_BMS);   // 0x0B
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_find_reg_map(void)
 * @Description: 测试寄存器映射查找函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_find_reg_map(void)
{
    TEST_START();
    
    const stc_cd_reg_map_t* reg_map;
    
    // 测试存在的寄存器
    reg_map = cd_can_find_reg_map(0);
    TEST_ASSERT_NOT_NULL(reg_map);
    TEST_ASSERT_EQUAL_UINT16(0, reg_map->reg_addr);
    
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    TEST_ASSERT_EQUAL_UINT16(10, reg_map->reg_addr);
    
    reg_map = cd_can_find_reg_map(5000);
    TEST_ASSERT_NOT_NULL(reg_map);
    TEST_ASSERT_EQUAL_UINT16(5000, reg_map->reg_addr);
    
    // 测试不存在的寄存器
    reg_map = cd_can_find_reg_map(9999);
    TEST_ASSERT_NULL(reg_map);
    
    reg_map = cd_can_find_reg_map(0xFFFF);
    TEST_ASSERT_NULL(reg_map);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 数据转换函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_transform_speed(void)
 * @Description: 测试速度数据转换函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_transform_speed(void)
{
    TEST_START();
    
    uint8_t data[4];
    uint8_t result;
    uint16_t test_speed = 50;  // 50 m/s
    
    // 测试读取转换
    result = cd_transform_speed(CD_TRANSFORM_READ, &test_speed, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    
    // 验证转换结果: 50 m/s * 360 = 18000 (km/h*100)
    uint16_t expected_speed_kmh_100 = 18000;
    uint16_t actual_speed_kmh_100 = (data[0] << 8) | data[1];
    TEST_ASSERT_EQUAL_UINT16(expected_speed_kmh_100, actual_speed_kmh_100);
    
    // 测试边界值
    test_speed = 0;
    result = cd_transform_speed(CD_TRANSFORM_READ, &test_speed, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    actual_speed_kmh_100 = (data[0] << 8) | data[1];
    TEST_ASSERT_EQUAL_UINT16(0, actual_speed_kmh_100);
    
    // 测试最大值限制 (300 km/h = 30000)
    test_speed = 1000;  // 1000 m/s，应该被限制到300 km/h
    result = cd_transform_speed(CD_TRANSFORM_READ, &test_speed, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    actual_speed_kmh_100 = (data[0] << 8) | data[1];
    TEST_ASSERT_EQUAL_UINT16(30000, actual_speed_kmh_100);
    
    // 测试写入转换（应该失败，因为5023是只读寄存器）
    data[0] = 0x00;
    data[1] = 0x64;  // 100 km/h*100
    result = cd_transform_speed(CD_TRANSFORM_WRITE, &test_speed, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试空指针
    result = cd_transform_speed(CD_TRANSFORM_READ, NULL, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    result = cd_transform_speed(CD_TRANSFORM_READ, &test_speed, NULL, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试缓冲区长度不足
    result = cd_transform_speed(CD_TRANSFORM_READ, &test_speed, data, 1);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_transform_gps_speed_limit(void)
 * @Description: 测试GPS速度限制转换函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_transform_gps_speed_limit(void)
{
    TEST_START();
    
    uint8_t data[4];
    uint8_t result;
    uint16_t test_speed_limit = 30;  // 30 m/s
    
    // 测试读取转换
    result = cd_transform_gps_speed_limit(CD_TRANSFORM_READ, &test_speed_limit, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    
    // 验证转换结果: 30 m/s * 360 = 10800 (km/h*100)
    uint16_t expected_speed_limit_kmh_100 = 10800;
    uint16_t actual_speed_limit_kmh_100 = (data[0] << 8) | data[1];
    TEST_ASSERT_EQUAL_UINT16(expected_speed_limit_kmh_100, actual_speed_limit_kmh_100);
    
    // 测试写入转换
    data[0] = 0x00;
    data[1] = 0x64;  // 100 km/h*100
    result = cd_transform_gps_speed_limit(CD_TRANSFORM_WRITE, &test_speed_limit, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 验证写入结果: 100 km/h*100 / 360 = 0.277... ≈ 0 m/s
    TEST_ASSERT_EQUAL_UINT16(0, test_speed_limit);
    
    // 测试边界值写入
    data[0] = 0x75;
    data[1] = 0x30;  // 30000 km/h*100 = 300 km/h
    result = cd_transform_gps_speed_limit(CD_TRANSFORM_WRITE, &test_speed_limit, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 验证写入结果: 30000 / 360 = 83.33... ≈ 83 m/s
    TEST_ASSERT_EQUAL_UINT16(83, test_speed_limit);
    
    // 测试超出范围的值
    data[0] = 0x75;
    data[1] = 0x31;  // 30001 km/h*100 > 300 km/h
    result = cd_transform_gps_speed_limit(CD_TRANSFORM_WRITE, &test_speed_limit, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试空指针
    result = cd_transform_gps_speed_limit(CD_TRANSFORM_READ, NULL, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    result = cd_transform_gps_speed_limit(CD_TRANSFORM_READ, &test_speed_limit, NULL, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试缓冲区长度不足
    result = cd_transform_gps_speed_limit(CD_TRANSFORM_READ, &test_speed_limit, data, 1);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 数据读写函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_read_data_from_ptr(void)
 * @Description: 测试从指针读取数据函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_read_data_from_ptr(void)
{
    TEST_START();
    
    uint8_t data[32];  // 增加缓冲区大小以容纳20字节
    uint8_t result;
    const stc_cd_reg_map_t* reg_map;
    
    // 测试uint16类型数据读取
    reg_map = cd_can_find_reg_map(0);
    TEST_ASSERT_NOT_NULL(reg_map);
    
    result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    
    // 测试uint8类型数据读取
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    
    result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);  // 即使uint8也返回2字节（寄存器大小）
    
    // 测试string类型数据读取
    reg_map = cd_can_find_reg_map(5002);  // ICCID
    TEST_ASSERT_NOT_NULL(reg_map);
    
    result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(20, result);  // 10个寄存器 * 2 = 20字节
    
    // 测试空指针
    result = cd_can_read_data_from_ptr(NULL, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    result = cd_can_read_data_from_ptr(reg_map, NULL, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试缓冲区长度不足
    result = cd_can_read_data_from_ptr(reg_map, data, 1);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_write_data_to_ptr(void)
 * @Description: 测试向指针写入数据函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_write_data_to_ptr(void)
{
    TEST_START();
    
    uint8_t data[8];
    uint8_t result;
    const stc_cd_reg_map_t* reg_map;
    
    // 测试uint16类型数据写入
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    
    data[0] = 0x12;
    data[1] = 0x34;
    result = cd_can_write_data_to_ptr(reg_map, data, 2);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试特殊寄存器写入（复位寄存器）
    reg_map = cd_can_find_reg_map(11);
    TEST_ASSERT_NOT_NULL(reg_map);
    
    data[0] = 0x00;
    data[1] = 0x01;  // 复位命令
    result = cd_can_write_data_to_ptr(reg_map, data, 2);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试恢复出厂设置
    reg_map = cd_can_find_reg_map(12);
    TEST_ASSERT_NOT_NULL(reg_map);
    
    data[0] = 0xAC;
    data[1] = 0x52;  // 恢复出厂设置命令
    result = cd_can_write_data_to_ptr(reg_map, data, 2);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试无效的恢复出厂设置值
    data[0] = 0x00;
    data[1] = 0x00;  // 无效值
    result = cd_can_write_data_to_ptr(reg_map, data, 2);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试清除用户数据
    reg_map = cd_can_find_reg_map(13);
    TEST_ASSERT_NOT_NULL(reg_map);
    
    data[0] = 0x00;
    data[1] = 0x01;  // 清除用户数据命令
    result = cd_can_write_data_to_ptr(reg_map, data, 2);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试空指针
    result = cd_can_write_data_to_ptr(NULL, data, 2);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    result = cd_can_write_data_to_ptr(reg_map, NULL, 2);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试数据长度为0
    result = cd_can_write_data_to_ptr(reg_map, data, 0);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 寄存器操作函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_read_register(void)
 * @Description: 测试读取寄存器函数（使用优化后的底层函数）
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_read_register(void)
{
    TEST_START();
    
    uint8_t data[32];  // 增加缓冲区大小以容纳20字节
    uint8_t result;
    const stc_cd_reg_map_t *reg_map;
    
    // 测试存在的寄存器
    reg_map = cd_can_find_reg_map(0);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    
    reg_map = cd_can_find_reg_map(5002);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(20, result);
    
    // 测试不存在的寄存器
    reg_map = cd_can_find_reg_map(9999);
    TEST_ASSERT_NULL(reg_map);
    
    // 测试空指针
    reg_map = cd_can_find_reg_map(0);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_read_data_from_ptr(reg_map, NULL, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试缓冲区长度为0
    reg_map = cd_can_find_reg_map(0);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_read_data_from_ptr(reg_map, data, 0);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_write_register(void)
 * @Description: 测试写入寄存器函数（使用优化后的底层函数）
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_write_register(void)
{
    TEST_START();
    
    uint8_t data[8];
    uint8_t result;
    const stc_cd_reg_map_t *reg_map;
    
    // 测试存在的寄存器
    data[0] = 0x12;
    data[1] = 0x34;
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_write_data_to_ptr(reg_map, data, 2);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试不存在的寄存器
    reg_map = cd_can_find_reg_map(9999);
    TEST_ASSERT_NULL(reg_map);
    
    // 测试空指针
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_write_data_to_ptr(reg_map, NULL, 2);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试数据长度为0
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_write_data_to_ptr(reg_map, data, 0);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_is_reg_readable(void)
 * @Description: 测试寄存器可读性检查函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_is_reg_readable(void)
{
    TEST_START();
    
    uint8_t result;
    
    // 测试只读寄存器
    result = cd_can_is_reg_readable(0);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    result = cd_can_is_reg_readable(1);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试读写寄存器
    result = cd_can_is_reg_readable(9);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    result = cd_can_is_reg_readable(10);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试只写寄存器（应该不可读）
    result = cd_can_is_reg_readable(5);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试不存在的寄存器
    result = cd_can_is_reg_readable(9999);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_is_reg_writable(void)
 * @Description: 测试寄存器可写性检查函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_is_reg_writable(void)
{
    TEST_START();
    
    uint8_t result;
    
    // 测试只写寄存器
    result = cd_can_is_reg_writable(5);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    result = cd_can_is_reg_writable(6);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试读写寄存器
    result = cd_can_is_reg_writable(9);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    result = cd_can_is_reg_writable(10);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试只读寄存器（应该不可写）
    result = cd_can_is_reg_writable(0);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    result = cd_can_is_reg_writable(1);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试不存在的寄存器
    result = cd_can_is_reg_writable(9999);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_performance_optimization(void)
 * @Description: 测试性能优化效果（比较优化前后的函数调用次数）
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_performance_optimization(void)
{
    TEST_START();
    
    uint8_t data[32];
    uint8_t result;
    const stc_cd_reg_map_t *reg_map;
    uint32_t start_time, end_time, elapsed_time;
    
    printf("Testing performance optimization...\n");
    
    // 测试优化后的直接调用方式
    start_time = mock_drv_systick_getTick();
    
    // 模拟多次寄存器操作（优化后：每次只查找一次reg_map）
    for (int i = 0; i < 100; i++)
    {
        reg_map = cd_can_find_reg_map(0);  // 只查找一次
        if (reg_map != NULL)
        {
            result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
            TEST_ASSERT_EQUAL_UINT8(2, result);
        }
        
        reg_map = cd_can_find_reg_map(10);  // 只查找一次
        if (reg_map != NULL)
        {
            result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
            TEST_ASSERT_EQUAL_UINT8(2, result);
        }
    }
    
    end_time = mock_drv_systick_getTick();
    elapsed_time = end_time - start_time;
    
    printf("Optimized approach: %d ms for 200 operations\n", elapsed_time);
    
    // 验证优化效果：应该比原来的方式更快
    // 这里我们主要验证功能正确性，性能提升在实际硬件上会更明显
    TEST_ASSERT(elapsed_time < 1000);  // 应该在1秒内完成
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_direct_function_calls(void)
 * @Description: 测试直接使用底层函数的正确性
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_direct_function_calls(void)
{
    TEST_START();
    
    uint8_t data[32];
    uint8_t result;
    const stc_cd_reg_map_t *reg_map;
    
    // 测试直接使用cd_can_read_data_from_ptr
    reg_map = cd_can_find_reg_map(0);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_read_data_from_ptr(reg_map, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(2, result);
    
    // 测试直接使用cd_can_write_data_to_ptr
    data[0] = 0xAB;
    data[1] = 0xCD;
    reg_map = cd_can_find_reg_map(10);
    TEST_ASSERT_NOT_NULL(reg_map);
    result = cd_can_write_data_to_ptr(reg_map, data, 2);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试空指针处理
    result = cd_can_read_data_from_ptr(NULL, data, sizeof(data));
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    result = cd_can_write_data_to_ptr(NULL, data, 2);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 测试套件实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_suite_basic_functions(void)
 * @Description: 基础工具函数测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_suite_basic_functions(void)
{
    TEST_SUITE_START("Basic Functions");
    
    test_cd_can_build_id();
    test_cd_can_parse_id();
    test_cd_can_is_for_me();
    test_cd_can_find_reg_map();
    
    TEST_SUITE_END("Basic Functions");
}

/*
 * @Functions:   void test_suite_data_transform(void)
 * @Description: 数据转换函数测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_suite_data_transform(void)
{
    TEST_SUITE_START("Data Transform Functions");
    
    test_cd_transform_speed();
    test_cd_transform_gps_speed_limit();
    test_cd_can_read_data_from_ptr();
    test_cd_can_write_data_to_ptr();
    
    TEST_SUITE_END("Data Transform Functions");
}

/*
 * @Functions:   void test_suite_register_operations(void)
 * @Description: 寄存器操作函数测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_suite_register_operations(void)
{
    TEST_SUITE_START("Register Operations");
    
    test_cd_can_read_register();
    test_cd_can_write_register();
    test_cd_can_is_reg_readable();
    test_cd_can_is_reg_writable();
    test_cd_can_performance_optimization();
    test_cd_can_direct_function_calls();
    
    TEST_SUITE_END("Register Operations");
}

//----------------------------------------------------------------------------------------
// 发送响应函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_send_read_response(void)
 * @Description: 测试发送读寄存器响应函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_send_read_response(void)
{
    TEST_START();
    
    uint8_t data[4] = {0x12, 0x34, 0x56, 0x78};
    uint8_t result;
    uint8_t tx_count_before, tx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试成功响应
    tx_count_before = mock_can_get_tx_count();
    result = cd_can_send_read_response(0x0001, 0x0002, 0x00, data, CD_ERROR_SUCCESS);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证CAN消息内容
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT32(0x030001AB, msg->ExtId);  // 读响应ID
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[1]);  // 寄存器数量高字节
    TEST_ASSERT_EQUAL_UINT8(0x02, msg->Data[2]);  // 寄存器数量低字节
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[3]);  // 帧索引
    
    // 使用新的应答帧检测函数
    test_verify_response_frame(CD_FUNC_READ_REG, 0x00, 0x02, 0x00, "Read Response Success");
    
    // 测试错误响应
    mock_can_reset();
    tx_count_before = mock_can_get_tx_count();
    result = cd_can_send_error_response(CD_FUNC_READ_REG_ERROR, 0x0001, 0x0002, 0x00, CD_ERROR_ILLEGAL_ADDR);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证错误响应消息
    msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_ADDR, msg->Data[4]);  // 错误码位置
    
    // 使用新的错误应答帧检测函数
    test_verify_error_response_frame(CD_FUNC_READ_REG_ERROR, CD_ERROR_ILLEGAL_ADDR, "Read Response Error");
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_send_write_response(void)
 * @Description: 测试发送写寄存器响应函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_send_write_response(void)
{
    TEST_START();
    
    uint8_t result;
    uint8_t tx_count_before, tx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试成功响应
    tx_count_before = mock_can_get_tx_count();
    result = cd_can_send_write_response(0x0001, 0x0002, 0x00, CD_ERROR_SUCCESS);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证CAN消息内容
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT32(0x100001AB, msg->ExtId);  // 写响应ID
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_WRITE_REG, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[1]);  // 寄存器数量高字节
    TEST_ASSERT_EQUAL_UINT8(0x02, msg->Data[2]);  // 寄存器数量低字节
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[3]);  // 帧索引
    
    // 使用新的应答帧检测函数
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x02, 0x00, "Write Response Success");
    
    // 验证数据填充
    for (int i = 4; i < 8; i++)
    {
        TEST_ASSERT_EQUAL_UINT8(CD_DATA_FILL, msg->Data[i]);
    }
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_send_error_response(void)
 * @Description: 测试发送错误响应函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_send_error_response(void)
{
    TEST_START();
    
    uint8_t result;
    uint8_t tx_count_before, tx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试读寄存器错误响应
    tx_count_before = mock_can_get_tx_count();
    result = cd_can_send_error_response(CD_FUNC_READ_REG_ERROR, 0x0001, 0x0002, 0x00, CD_ERROR_ILLEGAL_ADDR);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证CAN消息内容
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT32(0x830001AB, msg->ExtId);  // 读错误响应ID
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG_ERROR, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_ADDR, msg->Data[4]);
    
    // 使用新的错误应答帧检测函数
    test_verify_error_response_frame(CD_FUNC_READ_REG_ERROR, CD_ERROR_ILLEGAL_ADDR, "Read Error Response");
    
    // 测试写寄存器错误响应
    mock_can_reset();
    tx_count_before = mock_can_get_tx_count();
    result = cd_can_send_error_response(CD_FUNC_WRITE_REG_ERROR, 0x0001, 0x0002, 0x00, CD_ERROR_ILLEGAL_VALUE);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证CAN消息内容
    msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT32(0x900001AB, msg->ExtId);  // 写错误响应ID
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_WRITE_REG_ERROR, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_VALUE, msg->Data[4]);
    
    // 使用新的错误应答帧检测函数
    test_verify_error_response_frame(CD_FUNC_WRITE_REG_ERROR, CD_ERROR_ILLEGAL_VALUE, "Write Error Response");
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_send_event_report(void)
 * @Description: 测试发送事件上报函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_send_event_report(void)
{
    TEST_START();
    
    uint8_t data[4] = {0xAA, 0xBB, 0xCC, 0xDD};
    uint8_t result;
    uint8_t tx_count_before, tx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试事件上报
    tx_count_before = mock_can_get_tx_count();
    result = cd_can_send_event_report(0x0001, data);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证CAN消息内容
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT32(0x640001AB, msg->ExtId);  // 事件上报ID
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_EVENT_REPORT, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[1]);  // 寄存器数量高字节
    TEST_ASSERT_EQUAL_UINT8(0x01, msg->Data[2]);  // 寄存器数量低字节
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[3]);  // 帧索引
    
    // 使用新的应答帧检测函数
    test_verify_response_frame(CD_FUNC_EVENT_REPORT, 0x00, 0x01, 0x00, "Event Report");
    
    // 验证数据内容
    for (int i = 0; i < 4; i++)
    {
        TEST_ASSERT_EQUAL_UINT8(data[i], msg->Data[4 + i]);
    }
    
    // 测试空数据事件上报
    mock_can_reset();
    tx_count_before = mock_can_get_tx_count();
    result = cd_can_send_event_report(0x0001, NULL);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(1, result);
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证空数据填充
    msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    for (int i = 4; i < 8; i++)
    {
        TEST_ASSERT_EQUAL_UINT8(CD_DATA_FILL, msg->Data[i]);
    }
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_send_multi_frame_data(void)
 * @Description: 测试发送多帧数据函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_send_multi_frame_data(void)
{
    TEST_START();
    
    uint8_t data[12] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC};
    uint8_t tx_count_before, tx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试多帧数据发送
    tx_count_before = mock_can_get_tx_count();
    cd_can_send_multi_frame_data(0x0001, 0x0006, data, 12, CD_FUNC_READ_REG);
    tx_count_after = mock_can_get_tx_count();
    
    // 验证发送了3帧（12字节 / 4字节每帧 = 3帧）
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 3, tx_count_after);
    
    // 验证第一帧
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT32(0x030001AB, msg->ExtId);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[1]);  // 寄存器数量高字节
    TEST_ASSERT_EQUAL_UINT8(0x06, msg->Data[2]);  // 寄存器数量低字节
    TEST_ASSERT_EQUAL_UINT8(0x00, msg->Data[3]);  // 帧索引
    
    // 使用新的应答帧检测函数（多帧数据测试需要特殊处理）
    // 注意：多帧数据测试发送了3帧，所以需要分别验证每一帧
    uint8_t tx_count = mock_can_get_tx_count();
    TEST_ASSERT_EQUAL_UINT8(3, tx_count);  // 应该发送3个应答帧
    
    // 验证第一帧
    stc_can_message_t* msg1 = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg1);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG, msg1->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg1->Data[1]);
    TEST_ASSERT_EQUAL_UINT8(0x06, msg1->Data[2]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg1->Data[3]);
    printf("  ✓ Multi-frame Data Frame 1: Response frame verified (Func=0x%02X, Regs=%d, Frame=%d)\n", 
           CD_FUNC_READ_REG, 6, 0);
    
    // 验证第二帧
    stc_can_message_t* msg2 = mock_can_get_tx_message(1);
    TEST_ASSERT_NOT_NULL(msg2);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG, msg2->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg2->Data[1]);
    TEST_ASSERT_EQUAL_UINT8(0x06, msg2->Data[2]);
    TEST_ASSERT_EQUAL_UINT8(0x01, msg2->Data[3]);
    printf("  ✓ Multi-frame Data Frame 2: Response frame verified (Func=0x%02X, Regs=%d, Frame=%d)\n", 
           CD_FUNC_READ_REG, 6, 1);
    
    // 验证第三帧
    stc_can_message_t* msg3 = mock_can_get_tx_message(2);
    TEST_ASSERT_NOT_NULL(msg3);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG, msg3->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(0x00, msg3->Data[1]);
    TEST_ASSERT_EQUAL_UINT8(0x06, msg3->Data[2]);
    TEST_ASSERT_EQUAL_UINT8(0x02, msg3->Data[3]);
    printf("  ✓ Multi-frame Data Frame 3: Response frame verified (Func=0x%02X, Regs=%d, Frame=%d)\n", 
           CD_FUNC_READ_REG, 6, 2);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 写入处理函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_process_single_frame_write(void)
 * @Description: 测试单帧写入处理函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_process_single_frame_write(void)
{
    TEST_START();
    
    uint8_t data[4] = {0x12, 0x34, 0x00, 0x01};  // 寄存器11使用有效的复位值1
    uint8_t result;
    
    // 测试正常单帧写入
    result = cd_can_process_single_frame_write(0x000A, 0x0002, data);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_SUCCESS, result);
    
    // 测试寄存器数量超限
    result = cd_can_process_single_frame_write(0x000A, 0x0003, data);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_VALUE, result);
    
    // 测试不存在的寄存器
    result = cd_can_process_single_frame_write(0x9999, 0x0001, data);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_SLAVE_FAILURE, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_write_multi_register_range(void)
 * @Description: 测试多寄存器范围写入函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_write_multi_register_range(void)
{
    TEST_START();
    
    uint8_t data_buffer[8] = {0x11, 0x22, 0x00, 0x01, 0xAC, 0x52, 0x00, 0x01};  // 寄存器11使用0x0001，寄存器12使用0xAC52，寄存器13使用0x0001
    uint8_t result;
    
    // 测试正常多寄存器写入
    result = cd_can_write_multi_register_range(0x000A, 0x0004, data_buffer);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 测试不存在的寄存器范围
    result = cd_can_write_multi_register_range(0x9999, 0x0002, data_buffer);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_process_multi_frame_write(void)
 * @Description: 测试多帧写入处理函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_process_multi_frame_write(void)
{
    TEST_START();
    
    uint8_t data[4] = {0x00, 0x01, 0x00, 0x01};  // 寄存器10和11都使用有效的复位值1
    uint8_t result;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 测试第一帧 - 只测试2个寄存器
    result = cd_can_process_multi_frame_write(0x000A, 0x0002, 0x00, data);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_SUCCESS, result);
    
    // 测试第二帧 - 测试另外2个寄存器
    data[0] = 0xAC; data[1] = 0x52; data[2] = 0x00; data[3] = 0x01;  // 寄存器12使用0xAC52，寄存器13使用0x0001
    result = cd_can_process_multi_frame_write(0x000A, 0x0002, 0x01, data);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_SUCCESS, result);
    
    // 测试帧索引不匹配
    result = cd_can_process_multi_frame_write(0x000A, 0x0004, 0x03, data);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_VALUE, result);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 消息处理函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_process_read_request(void)
 * @Description: 测试读寄存器请求处理函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_process_read_request(void)
{
    TEST_START();
    
    uint8_t data[4] = {0x00, 0x00, 0x00, 0x00};
    uint8_t tx_count_before, tx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试正常读请求
    tx_count_before = mock_can_get_tx_count();
    cd_can_process_read_request(0x0000, 0x0001, 0x00, data);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 测试不可读寄存器
    mock_can_reset();
    tx_count_before = mock_can_get_tx_count();
    cd_can_process_read_request(0x0005, 0x0001, 0x00, data);  // 只写寄存器
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证发送了错误响应
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG_ERROR, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_ADDR, msg->Data[4]);
    
    // 测试寄存器数量超限
    mock_can_reset();
    tx_count_before = mock_can_get_tx_count();
    cd_can_process_read_request(0x0000, 0x0100, 0x00, data);  // 256个寄存器
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证发送了错误响应
    msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_READ_REG_ERROR, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_VALUE, msg->Data[4]);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_process_write_request(void)
 * @Description: 测试写寄存器请求处理函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_process_write_request(void)
{
    TEST_START();
    
    uint8_t data[4] = {0x12, 0x34, 0x00, 0x01};  // 寄存器11使用有效的复位值1
    uint8_t tx_count_before, tx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试正常写请求
    tx_count_before = mock_can_get_tx_count();
    cd_can_process_write_request(0x000A, 0x0002, 0x00, data);
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证发送了成功响应
    stc_can_message_t* msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_WRITE_REG, msg->Data[0]);
    
    // 测试不可写寄存器
    mock_can_reset();
    tx_count_before = mock_can_get_tx_count();
    cd_can_process_write_request(0x0000, 0x0001, 0x00, data);  // 只读寄存器
    tx_count_after = mock_can_get_tx_count();
    
    TEST_ASSERT_EQUAL_UINT8(tx_count_before + 1, tx_count_after);
    
    // 验证发送了错误响应
    msg = mock_can_get_tx_message(0);
    TEST_ASSERT_NOT_NULL(msg);
    TEST_ASSERT_EQUAL_UINT8(CD_FUNC_WRITE_REG_ERROR, msg->Data[0]);
    TEST_ASSERT_EQUAL_UINT8(CD_ERROR_ILLEGAL_ADDR, msg->Data[4]);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_process_upgrade_message(void)
 * @Description: 测试升级消息处理函数（通过转换函数）
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_process_upgrade_message(void)
{
    TEST_START();
    
    uint8_t data[4];
    const stc_cd_reg_map_t *reg_map;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 先设置固件大小和CRC
    reg_map = cd_can_find_reg_map(303);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0x00; data[1] = 0x00; data[2] = 0x10; data[3] = 0x00;  // 4096字节
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 4));
    
    reg_map = cd_can_find_reg_map(305);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0x12; data[1] = 0x34;  // CRC值
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 2));
    
    // 测试开始升级命令
    reg_map = cd_can_find_reg_map(300);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0x00; data[1] = 0x01;
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 2));
    
    // 测试直接运行app命令
    reg_map = cd_can_find_reg_map(301);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0x00; data[1] = 0x01;
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 2));
    
    // 测试进入升级模式命令
    reg_map = cd_can_find_reg_map(302);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0x00; data[1] = 0x01;
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 2));
    
    // 测试退出升级模式命令
    data[0] = 0x00; data[1] = 0x02;
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 2));
    
    // 测试升级包大小
    reg_map = cd_can_find_reg_map(303);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0x00; data[1] = 0x00; data[2] = 0x10; data[3] = 0x00;  // 4096字节
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 4));
    
    // 测试升级包CRC校验
    reg_map = cd_can_find_reg_map(305);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0x12; data[1] = 0x34;
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 2));
    
    // 测试固件数据下发
    reg_map = cd_can_find_reg_map(315);
    TEST_ASSERT_NOT_NULL(reg_map);
    data[0] = 0xAA; data[1] = 0xBB; data[2] = 0xCC; data[3] = 0xDD;
    TEST_ASSERT_EQUAL_UINT8(1, cd_can_write_data_to_ptr(reg_map, data, 4));
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 升级相关函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_start_upgrade(void)
 * @Description: 测试开始升级函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_start_upgrade(void)
{
    TEST_START();
    
    uint8_t result;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 测试正常升级开始
    result = cd_can_start_upgrade(4096, 0x1234);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 验证升级状态
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_PREPARE, cd_can_get_upgrade_state());
    
    // 测试固件大小为零
    result = cd_can_start_upgrade(0, 0x1234);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试固件大小超限
    result = cd_can_start_upgrade(CD_MAX_FIRMWARE_SIZE + 1, 0x1234);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_process_upgrade_data(void)
 * @Description: 测试处理升级数据函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_process_upgrade_data(void)
{
    TEST_START();
    
    uint8_t data[4] = {0xAA, 0xBB, 0xCC, 0xDD};
    uint8_t result;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 先开始升级
    cd_can_start_upgrade(4096, 0x1234);
    
    // 测试正常数据处理
    result = cd_can_process_upgrade_data(data, 4);
    TEST_ASSERT_EQUAL_UINT8(1, result);
    
    // 验证升级状态
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_RECEIVE, cd_can_get_upgrade_state());
    
    // 测试空指针
    result = cd_can_process_upgrade_data(NULL, 4);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试数据长度为0
    result = cd_can_process_upgrade_data(data, 0);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 测试未激活升级
    cd_can_init(CD_DEVICE_ADDR_BMS);  // 重新初始化，清除升级状态
    result = cd_can_process_upgrade_data(data, 4);
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_finish_upgrade(void)
 * @Description: 测试完成升级函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_finish_upgrade(void)
{
    TEST_START();
    
    uint8_t result;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 测试未激活升级
    result = cd_can_finish_upgrade();
    TEST_ASSERT_EQUAL_UINT8(0, result);
    
    // 开始升级并接收数据
    cd_can_start_upgrade(8, 0x1234);  // 8字节固件
    
    uint8_t data[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
    cd_can_process_upgrade_data(data, 8);
    
    // 测试CRC验证失败（模拟错误的CRC）
    result = cd_can_finish_upgrade();
    TEST_ASSERT_EQUAL_UINT8(0, result);
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_ERROR, cd_can_get_upgrade_state());
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_get_upgrade_state(void)
 * @Description: 测试获取升级状态函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_get_upgrade_state(void)
{
    TEST_START();
    
    uint8_t state;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 测试初始状态
    state = cd_can_get_upgrade_state();
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_IDLE, state);
    
    // 开始升级
    cd_can_start_upgrade(4096, 0x1234);
    state = cd_can_get_upgrade_state();
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_PREPARE, state);
    
    // 接收数据
    uint8_t data[4] = {0x11, 0x22, 0x33, 0x44};
    cd_can_process_upgrade_data(data, 4);
    state = cd_can_get_upgrade_state();
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_RECEIVE, state);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 统计信息函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_get_stats(void)
 * @Description: 测试获取统计信息函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_get_stats(void)
{
    TEST_START();
    
    uint32_t rx_count, tx_count, error_count, timeout_count;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 获取初始统计信息
    cd_can_get_stats(&rx_count, &tx_count, &error_count, &timeout_count);
    TEST_ASSERT_EQUAL_UINT32(0, rx_count);
    TEST_ASSERT_EQUAL_UINT32(0, tx_count);
    TEST_ASSERT_EQUAL_UINT32(0, error_count);
    TEST_ASSERT_EQUAL_UINT32(0, timeout_count);
    
    // 测试部分参数为NULL
    cd_can_get_stats(NULL, &tx_count, &error_count, &timeout_count);
    cd_can_get_stats(&rx_count, NULL, &error_count, &timeout_count);
    cd_can_get_stats(&rx_count, &tx_count, NULL, &timeout_count);
    cd_can_get_stats(&rx_count, &tx_count, &error_count, NULL);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_reset_stats(void)
 * @Description: 测试重置统计信息函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_reset_stats(void)
{
    TEST_START();
    
    uint32_t rx_count, tx_count, error_count, timeout_count;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置统计信息
    cd_can_reset_stats();
    
    // 验证统计信息已重置
    cd_can_get_stats(&rx_count, &tx_count, &error_count, &timeout_count);
    TEST_ASSERT_EQUAL_UINT32(0, rx_count);
    TEST_ASSERT_EQUAL_UINT32(0, tx_count);
    TEST_ASSERT_EQUAL_UINT32(0, error_count);
    TEST_ASSERT_EQUAL_UINT32(0, timeout_count);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 对外接口函数测试实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_cd_can_invalid_src_addr(void)
 * @Description: 测试非法源地址检查
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_invalid_src_addr(void)
{
    TEST_START();
    
    uint8_t data[8] = {CD_FUNC_READ_REG, 0x00, 0x01, 0x00, 0x12, 0x34, 0x56, 0x78};
    uint32_t error_count_before, error_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 获取初始错误计数
    cd_can_get_stats(NULL, NULL, &error_count_before, NULL);
    
    // 测试非法源地址 (0x0D 不在合法地址范围内)
    uint32_t can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0001, CD_DEVICE_ADDR_BMS, 0x0D);
    cd_can_process_message(can_id, data, 8);
    
    // 获取处理后的错误计数
    cd_can_get_stats(NULL, NULL, &error_count_after, NULL);
    
    // 验证错误计数增加
    TEST_ASSERT_EQUAL_UINT32(error_count_before + 1, error_count_after);
    
    // 测试合法源地址 (应该正常处理)
    error_count_before = error_count_after;
    can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0001, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    cd_can_process_message(can_id, data, 8);
    
    cd_can_get_stats(NULL, NULL, &error_count_after, NULL);
    
    // 验证错误计数没有增加
    TEST_ASSERT_EQUAL_UINT32(error_count_before, error_count_after);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_init(void)
 * @Description: 测试初始化函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_init(void)
{
    TEST_START();
    
    // 测试正常初始化
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 验证设备地址设置
    TEST_ASSERT_EQUAL_UINT8(CD_DEVICE_ADDR_BMS, g_stcCdCan.device_addr);
    
    // 验证协议版本设置
    TEST_ASSERT_EQUAL_UINT8(0x00, g_stcCdCan.protocol_version);  // CD_CAN_PROTOCOL_VERSION & 0xFF
    
    // 验证多帧传输管理初始化
    TEST_ASSERT_NOT_NULL(g_stcCdCan.multi_frame_rx.data_buffer);
    TEST_ASSERT_EQUAL_UINT16(CD_MAX_MULTI_FRAME_SIZE, g_stcCdCan.multi_frame_rx.buffer_size);
    
    TEST_ASSERT_NOT_NULL(g_stcCdCan.multi_frame_tx.data_buffer);
    TEST_ASSERT_EQUAL_UINT16(CD_MAX_MULTI_FRAME_SIZE, g_stcCdCan.multi_frame_tx.buffer_size);
    
    // 验证升级管理初始化
    TEST_ASSERT_NOT_NULL(g_stcCdCan.upgrade.firmware_buffer);
    TEST_ASSERT_EQUAL_UINT32(CD_MAX_FIRMWARE_SIZE, g_stcCdCan.upgrade.buffer_size);
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_IDLE, g_stcCdCan.upgrade.upgrade_state);
    
    // 验证统计信息初始化
    TEST_ASSERT_EQUAL_UINT32(0, g_stcCdCan.stats.rx_count);
    TEST_ASSERT_EQUAL_UINT32(0, g_stcCdCan.stats.tx_count);
    TEST_ASSERT_EQUAL_UINT32(0, g_stcCdCan.stats.error_count);
    TEST_ASSERT_EQUAL_UINT32(0, g_stcCdCan.stats.timeout_count);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_process_message(void)
 * @Description: 测试消息处理函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_process_message(void)
{
    TEST_START();
    
    uint8_t data[8];
    uint32_t rx_count_before, rx_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 重置CAN发送队列
    mock_can_reset();
    
    // 测试读寄存器请求
    uint32_t can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0000, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    data[0] = CD_FUNC_READ_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    memset(&data[4], CD_DATA_FILL, 4);
    
    rx_count_before = g_stcCdCan.stats.rx_count;
    cd_can_process_message(can_id, data, 8);
    rx_count_after = g_stcCdCan.stats.rx_count;
    
    TEST_ASSERT_EQUAL_UINT32(rx_count_before + 1, rx_count_after);
    
    // 验证发送了响应
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());
    
    // 验证读取寄存器应答帧
    test_verify_response_frame(CD_FUNC_READ_REG, 0x00, 0x01, 0x00, "Process Message Read Response");
    
    // 测试写寄存器请求
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x000A, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    data[4] = 0x12; data[5] = 0x34;  // 数据
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    rx_count_before = g_stcCdCan.stats.rx_count;
    cd_can_process_message(can_id, data, 8);
    rx_count_after = g_stcCdCan.stats.rx_count;
    
    TEST_ASSERT_EQUAL_UINT32(rx_count_before + 1, rx_count_after);
    
    // 验证发送了响应
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());
    
    // 验证写入寄存器应答帧
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x01, 0x00, "Process Message Write Response");
    
    // 测试升级消息（使用写寄存器功能码）
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012C, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 300
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    data[4] = 0x00; data[5] = 0x01;  // 开始升级命令
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    rx_count_before = g_stcCdCan.stats.rx_count;
    cd_can_process_message(can_id, data, 8);
    rx_count_after = g_stcCdCan.stats.rx_count;
    
    TEST_ASSERT_EQUAL_UINT32(rx_count_before + 1, rx_count_after);
    
    // 测试非法功能码
    mock_can_reset();
    can_id = cd_can_build_id(0xFF, 0x0000, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    data[0] = 0xFF;  // 非法功能码
    data[1] = 0x00; data[2] = 0x01;
    data[3] = 0x00;
    memset(&data[4], CD_DATA_FILL, 4);
    
    rx_count_before = g_stcCdCan.stats.rx_count;
    cd_can_process_message(can_id, data, 8);
    rx_count_after = g_stcCdCan.stats.rx_count;
    
    TEST_ASSERT_EQUAL_UINT32(rx_count_before + 1, rx_count_after);
    
    // 验证发送了错误响应
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());
    
    // 验证非法功能码错误应答帧
    test_verify_error_response_frame(CD_FUNC_WRITE_REG_ERROR, CD_ERROR_ILLEGAL_FUNC, "Process Message Illegal Function Error");
    
    // 测试非目标设备消息（应该被忽略）
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0000, CD_DEVICE_ADDR_HOST, CD_DEVICE_ADDR_BMS);
    
    rx_count_before = g_stcCdCan.stats.rx_count;
    cd_can_process_message(can_id, data, 8);
    rx_count_after = g_stcCdCan.stats.rx_count;
    
    TEST_ASSERT_EQUAL_UINT32(rx_count_before + 1, rx_count_after);  // 接收计数增加
    TEST_ASSERT_EQUAL_UINT8(0, mock_can_get_tx_count());  // 但不发送响应
    
    // 测试数据长度不足
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0000, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    rx_count_before = g_stcCdCan.stats.rx_count;
    uint32_t error_count_before = g_stcCdCan.stats.error_count;
    cd_can_process_message(can_id, data, 3);  // 长度不足
    rx_count_after = g_stcCdCan.stats.rx_count;
    uint32_t error_count_after = g_stcCdCan.stats.error_count;
    
    TEST_ASSERT_EQUAL_UINT32(rx_count_before + 1, rx_count_after);
    TEST_ASSERT_EQUAL_UINT32(error_count_before + 1, error_count_after);
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_task(void)
 * @Description: 测试任务函数
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_task(void)
{
    TEST_START();
    
    uint32_t timeout_count_before, timeout_count_after;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    
    // 测试正常任务运行
    cd_can_task();
    
    // 测试多帧传输超时
    cd_can_start_upgrade(4096, 0x1234);
    uint8_t data[4] = {0x11, 0x22, 0x33, 0x44};
    cd_can_process_upgrade_data(data, 4);
    
    // 设置超时时间
    mock_drv_systick_setTick(CD_UPGRADE_TIMEOUT_MS + 100);
    
    timeout_count_before = g_stcCdCan.stats.timeout_count;
    cd_can_task();
    timeout_count_after = g_stcCdCan.stats.timeout_count;
    
    TEST_ASSERT_EQUAL_UINT32(timeout_count_before + 1, timeout_count_after);
    
    TEST_END();
}

//----------------------------------------------------------------------------------------
// 测试套件实现
//----------------------------------------------------------------------------------------

/*
 * @Functions:   void test_suite_message_processing(void)
 * @Description: 消息处理函数测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_suite_message_processing(void)
{
    TEST_SUITE_START("Message Processing");
    
    test_cd_can_send_read_response();
    test_cd_can_send_write_response();
    test_cd_can_send_error_response();
    test_cd_can_send_event_report();
    test_cd_can_send_multi_frame_data();
    test_cd_can_process_single_frame_write();
    test_cd_can_write_multi_register_range();
    test_cd_can_process_multi_frame_write();
    test_cd_can_process_read_request();
    test_cd_can_process_write_request();
    test_cd_can_process_upgrade_message();
    
    TEST_SUITE_END("Message Processing");
}

/*
 * @Functions:   void test_suite_upgrade_functions(void)
 * @Description: 升级相关函数测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_suite_upgrade_functions(void)
{
    TEST_SUITE_START("Upgrade Functions");
    
    test_cd_can_start_upgrade();
    test_cd_can_process_upgrade_data();
    test_cd_can_finish_upgrade();
    test_cd_can_get_upgrade_state();
    
    TEST_SUITE_END("Upgrade Functions");
}

/*
 * @Functions:   void test_suite_integration(void)
 * @Description: 集成测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_suite_integration(void)
{
    TEST_SUITE_START("Integration Tests");
    
    test_cd_can_get_stats();
    test_cd_can_reset_stats();
    test_cd_can_init();
    test_cd_can_process_message();  
    test_cd_can_task();
    
    TEST_SUITE_END("Integration Tests");
}

/*
 * @Functions:   void test_cd_can_complete_upgrade_flow(void)
 * @Description: 测试完整的升级流程 - 符合生产条件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_complete_upgrade_flow(void)
{
    TEST_START();
    
    uint8_t data[8];
    uint32_t can_id;
    uint32_t firmware_size = 10 * 1024;  // 100KB测试固件，模拟真实生产环境
    uint16_t firmware_crc = 0x1234; // 测试CRC
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    mock_can_reset();
    
    printf("=== Start Complete Upgrade Flow Test ===\n");
    
    // 步骤1: 设备上电后查询版本号
    printf("Step 1: Query Device Version\n");
    can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0000, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    data[0] = CD_FUNC_READ_REG;
    data[1] = 0x00; data[2] = 0x01;  // 读取1个寄存器：软件版本
    data[3] = 0x00;  // 帧索引
    memset(&data[4], CD_DATA_FILL, 4);
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送版本信息响应
    
    // 验证版本信息应答帧
    test_verify_response_frame(CD_FUNC_READ_REG, 0x00, 0x01, 0x00, "Version Query Response");
    
    // 步骤2: 发送进入升级模式指令（302）   
    printf("Step 2: Send Enter Upgrade Mode Command\n");
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012E, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 302
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    data[4] = 0x00; data[5] = 0x01;  // 进入升级模式
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送确认响应
    
    // 验证进入升级模式应答帧
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x01, 0x00, "Enter Upgrade Mode Response");
    
    // 模拟设备重启进入boot模式
    printf("Step 3: Device Restart into Boot Mode\n");
    mock_drv_systick_setTick(100);  // 模拟重启时间
    
    // 步骤4: 查询软件类型（5）判断是否在等待升级状态
    printf("Step 4: Query Software Type\n");    
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0005, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    data[0] = CD_FUNC_READ_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    memset(&data[4], CD_DATA_FILL, 4);
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送软件类型错误响应
    
    // 验证软件类型错误应答帧（寄存器5是只写寄存器）
    test_verify_error_response_frame(CD_FUNC_READ_REG_ERROR, CD_ERROR_ILLEGAL_ADDR, "Software Type Query Error Response");
    
    // 步骤5: 发送升级包大小（303）
    printf("Step 5: Send Upgrade Package Size\n");    
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012F, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 303
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x02;  // 2个寄存器（uint32）
    data[3] = 0x00;  // 帧索引
    data[4] = (firmware_size >> 24) & 0xFF;  // 大端存储
    data[5] = (firmware_size >> 16) & 0xFF;
    data[6] = (firmware_size >> 8) & 0xFF;
    data[7] = firmware_size & 0xFF;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送确认响应
    
    // 验证升级包大小应答帧
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x02, 0x00, "Firmware Size Response");
    
    // 步骤6: 发送升级包CRC校验（305）
    printf("Step 6: Send Upgrade Package CRC\n");    
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x0131, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 305
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    data[4] = (firmware_crc >> 8) & 0xFF;  // 大端存储
    data[5] = firmware_crc & 0xFF;
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送确认响应
    
    // 验证CRC校验应答帧
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x01, 0x00, "CRC Verification Response");
    
    // 步骤7: 发送开始升级命令（300）
    printf("Step 7: Send Start Upgrade Command\n");    
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012C, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 300
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    data[4] = 0x00; data[5] = 0x01;  // 开始升级
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送确认响应
    
    // 验证开始升级应答帧
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x01, 0x00, "Start Upgrade Response");
    
    // 验证升级状态（只有在固件大小和CRC都已设置的情况下才会进入PREPARE状态）
    // 由于我们之前已经设置了固件大小和CRC，所以应该进入PREPARE状态
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_PREPARE, cd_can_get_upgrade_state());
    
    // 步骤8: 发送固件数据包（315）- 模拟真实生产环境
    printf("Step 8: Send Firmware Data Package (100KB)\n");
    printf("Firmware Size: %d bytes, CRC: 0x%04X\n", firmware_size, firmware_crc);
    
    // 计算数据包信息（按照协议规定）
    uint32_t max_registers_per_write = 255;  // 一次最多写入256个寄存器
    uint32_t bytes_per_register = 2;         // 每个寄存器2字节
    uint32_t max_bytes_per_write = max_registers_per_write * bytes_per_register;  // 510字节
    uint32_t registers_per_frame = 2;        // 每帧最多2个寄存器（4字节数据）
    uint32_t frames_per_write = max_registers_per_write / registers_per_frame;  // 127帧
    
    uint32_t total_writes = (firmware_size + max_bytes_per_write - 1) / max_bytes_per_write;
    uint32_t current_offset = 0;
    
    printf("Protocol Analysis:\n");
    printf("  Max registers per write: %d\n", max_registers_per_write);
    printf("  Max bytes per write: %d\n", max_bytes_per_write);
    printf("  Frames per write: %d\n", frames_per_write);
    printf("  Total writes needed: %d\n", total_writes);
    printf("Starting firmware data transmission...\n");
    
    // 发送固件数据包（按照协议规定的多帧传输）
    for (uint32_t write_index = 0; write_index < total_writes; write_index++)
    {
        // 计算当前写入的数据大小
        uint32_t remaining_bytes = firmware_size - current_offset;
        uint32_t current_write_size = (remaining_bytes > max_bytes_per_write) ? max_bytes_per_write : remaining_bytes;
        uint32_t current_reg_count = current_write_size / bytes_per_register;  // 寄存器数量
        
        printf("  Write %d/%d: Offset=0x%08X, Size=%d bytes (%d registers)\n", 
               write_index + 1, total_writes, current_offset, current_write_size, current_reg_count);
        
        // 发送多帧数据（每帧2个寄存器）
        for (uint32_t frame_index = 0; frame_index < frames_per_write; frame_index++)
        {
            // 检查是否还有数据需要发送
            if (frame_index * registers_per_frame >= current_reg_count)
                break;
                
            mock_can_reset();
            can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x013B, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 315
            
            // 计算当前帧的寄存器数量
            // uint32_t remaining_regs = current_reg_count - (frame_index * registers_per_frame);
            // uint32_t frame_reg_count = (remaining_regs > registers_per_frame) ? registers_per_frame : remaining_regs;
            
            data[0] = CD_FUNC_WRITE_REG;
            data[1] = (current_reg_count >> 8) & 0xFF;  // 总寄存器数量（高字节）
            data[2] = current_reg_count & 0xFF;         // 总寄存器数量（低字节）
            data[3] = frame_index;                      // 帧索引
            
            // 发送实际固件数据（模拟payload）
            // 生成测试固件数据
            uint8_t test_data[4];
            for (int i = 0; i < 4; i++)
            {
                test_data[i] = (uint8_t)((current_offset + i) & 0xFF);
            }
            
            data[4] = test_data[0];
            data[5] = test_data[1];
            data[6] = test_data[2];
            data[7] = test_data[3];
            
            cd_can_process_message(can_id, data, 8);
            
            // 计算并显示进度
            // uint32_t progress_percent = ((current_offset + current_write_size) * 100) / firmware_size;
            // printf("    Frame %d/%d: Regs=%d, Progress=%d%%\n", 
            //        frame_index + 1, frames_per_write, frame_reg_count, progress_percent);
        }
        
        current_offset += current_write_size;
        
        // 注意：升级状态在多帧传输完成后才会更新
        
        // 模拟网络延迟（可选）
        // mock_drv_systick_setTick(mock_drv_systick_getTick() + 10);  // 10ms延迟
    }
    
    printf("Firmware data transmission completed: %d bytes in %d writes (%d total frames)\n", 
           firmware_size, total_writes, total_writes * frames_per_write);
    
    // 注意：升级状态在多帧传输完成后才会更新，这里暂时不检查状态
    // TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_RECEIVE, cd_can_get_upgrade_state());
    
    // 步骤9: 发送进入app指令（301）
    printf("Step 9: Send Enter App Command\n");    
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012D, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 301
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    data[4] = 0x00; data[5] = 0x01;  // 进入app
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送确认响应
    
    // 验证进入app应答帧
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x01, 0x00, "Enter App Response");
    
    // 验证升级完成状态
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_COMPLETE, cd_can_get_upgrade_state());
    
    // 步骤10: 查询软件版本号检查升级是否成功
    printf("Step 10: Query Upgraded Software Version\n");    
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_READ_REG, 0x0000, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);
    data[0] = CD_FUNC_READ_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;  // 帧索引
    memset(&data[4], CD_DATA_FILL, 4);
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送版本信息响应
    
    // 验证升级后版本信息应答帧
    test_verify_response_frame(CD_FUNC_READ_REG, 0x00, 0x01, 0x00, "Upgraded Version Query Response");
    
    printf("=== Complete Upgrade Flow Test Completed ===\n");
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_upgrade_timeout_scenarios(void)
 * @Description: 测试升级过程中的超时和错误处理
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_upgrade_timeout_scenarios(void)
{
    TEST_START();
    
    uint8_t data[8];
    uint32_t can_id;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    mock_can_reset();
    
    printf("=== Start Upgrade Timeout and Error Handling Test ===\n");
    
    // 测试1: 升级数据包超时
    printf("Test 1: Upgrade Data Package Timeout Handling\n");
    
    // 先设置固件大小和CRC
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012F, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 303
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x02;  // 2个寄存器
    data[3] = 0x00;
    data[4] = 0x00; data[5] = 0x00; data[6] = 0x04; data[7] = 0x00;  // 1024字节
    cd_can_process_message(can_id, data, 8);
    
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x0131, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 305
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;  // 1个寄存器
    data[3] = 0x00;
    data[4] = 0x12; data[5] = 0x34;  // CRC
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    cd_can_process_message(can_id, data, 8);
    
    // 启动升级
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012C, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 300
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;
    data[3] = 0x00;
    data[4] = 0x00; data[5] = 0x01;  // 开始升级
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_PREPARE, cd_can_get_upgrade_state());
    
    // 模拟超时（超过10秒）
    mock_drv_systick_setTick(CD_UPGRADE_TIMEOUT_MS + 100);
    cd_can_task();  // 处理超时
    
    TEST_ASSERT_EQUAL_UINT8(CD_UPGRADE_STATE_ERROR, cd_can_get_upgrade_state());
    
    // 测试2: 多帧传输超时
    printf("Test 2: Multi-frame Transmission Timeout Handling\n");
    
    // 重置状态
    cd_can_init(CD_DEVICE_ADDR_BMS);
    mock_can_reset();
    
    // 发送多帧数据（写入多个寄存器）
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x0005, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 写入寄存器5-8（可写）
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x04;  // 4个寄存器
    data[3] = 0x00;  // 帧索引0
    data[4] = 0x00; data[5] = 0x01; data[6] = 0x00; data[7] = 0x02;  // 数据
    
    cd_can_process_message(can_id, data, 8);
    
    // 模拟多帧传输超时（超过200ms）
    mock_drv_systick_setTick(CD_MULTI_FRAME_TIMEOUT_MS + 100);
    cd_can_task();  // 处理超时
    
    // 验证超时计数增加
    uint32_t timeout_count;
    cd_can_get_stats(NULL, NULL, NULL, &timeout_count);
    TEST_ASSERT_EQUAL_UINT32(1, timeout_count);
    
    // 测试3: 升级过程中的错误响应
    printf("Test 3: Error Response During Upgrade\n");
    
    // 重置状态
    cd_can_init(CD_DEVICE_ADDR_BMS);
    mock_can_reset();
    
    // 发送无效的升级命令
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012C, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 300
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;
    data[3] = 0x00;
    data[4] = 0x00; data[5] = 0x99;  // 无效的升级命令
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    
    // 验证发送了错误响应
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());
    
    // 验证升级超时错误应答帧
    test_verify_error_response_frame(CD_FUNC_WRITE_REG_ERROR, CD_ERROR_ILLEGAL_VALUE, "Upgrade Timeout Error Response");
    
    printf("=== Upgrade Timeout and Error Handling Test Completed ===\n");
    
    TEST_END();
}

/*
 * @Functions:   void test_cd_can_upgrade_data_integrity(void)
 * @Description: 测试升级数据完整性验证
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_cd_can_upgrade_data_integrity(void)
{
    TEST_START();
    
    uint8_t data[8];
    uint32_t can_id;
    uint32_t firmware_size = 1024;
    uint16_t firmware_crc = 0x1234;
    
    // 初始化模块
    cd_can_init(CD_DEVICE_ADDR_BMS);
    mock_can_reset();
    
    printf("=== Start Upgrade Data Integrity Test ===\n");
    
    // 测试1: 正确的CRC校验
    printf("Test 1: Correct CRC Verification\n");
    
    // 设置升级包大小
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012F, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 303
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x02;
    data[3] = 0x00;
    data[4] = (firmware_size >> 24) & 0xFF;
    data[5] = (firmware_size >> 16) & 0xFF;
    data[6] = (firmware_size >> 8) & 0xFF;
    data[7] = firmware_size & 0xFF;
    
    cd_can_process_message(can_id, data, 8);
    
    // 设置CRC校验
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x0131, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 305
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;
    data[3] = 0x00;
    data[4] = (firmware_crc >> 8) & 0xFF;
    data[5] = firmware_crc & 0xFF;
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该成功
    
    // 验证正确的CRC校验应答帧
    test_verify_response_frame(CD_FUNC_WRITE_REG, 0x00, 0x01, 0x00, "Correct CRC Verification Response");
    
    // 测试2: 错误的CRC校验
    printf("Test 2: Incorrect CRC Verification\n");
    
    // 重置状态
    cd_can_init(CD_DEVICE_ADDR_BMS);
    mock_can_reset();
    
    // 设置升级包大小
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012F, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 303
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x02;
    data[3] = 0x00;
    data[4] = (firmware_size >> 24) & 0xFF;
    data[5] = (firmware_size >> 16) & 0xFF;
    data[6] = (firmware_size >> 8) & 0xFF;
    data[7] = firmware_size & 0xFF;
    
    cd_can_process_message(can_id, data, 8);
    
    // 设置错误的CRC校验
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x0131, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 305
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;
    data[3] = 0x00;
    data[4] = 0xFF;  // 错误的CRC
    data[5] = 0xFF;
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送错误响应
    
    // 验证错误的CRC校验错误应答帧
    test_verify_error_response_frame(CD_FUNC_WRITE_REG_ERROR, CD_ERROR_ILLEGAL_VALUE, "Incorrect CRC Verification Error Response");
    
    // 测试3: 数据包顺序验证
    printf("Test 3: Data Packet Sequence Verification\n");    
    
    // 重置状态
    cd_can_init(CD_DEVICE_ADDR_BMS);
    mock_can_reset();
    
    // 启动升级
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x012C, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 300
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x01;
    data[3] = 0x00;
    data[4] = 0x00; data[5] = 0x01;
    data[6] = CD_DATA_FILL; data[7] = CD_DATA_FILL;
    
    cd_can_process_message(can_id, data, 8);
    
    // 发送错误顺序的数据包（先发送第2包）
    mock_can_reset();
    can_id = cd_can_build_id(CD_FUNC_WRITE_REG, 0x013B, CD_DEVICE_ADDR_BMS, CD_DEVICE_ADDR_HOST);  // 315
    data[0] = CD_FUNC_WRITE_REG;
    data[1] = 0x00; data[2] = 0x03;  // 3个寄存器
    data[3] = 0x02;  // 第2包（错误顺序）
    data[4] = 0x00; data[5] = 0x00; data[6] = 0x00; data[7] = 0x00;  // 偏移量0
    
    cd_can_process_message(can_id, data, 8);
    TEST_ASSERT_EQUAL_UINT8(1, mock_can_get_tx_count());  // 应该发送错误响应
    
    // 验证数据包顺序错误应答帧
    test_verify_error_response_frame(CD_FUNC_WRITE_REG_ERROR, CD_ERROR_ILLEGAL_VALUE, "Data Packet Sequence Error Response");
    
    printf("=== Upgrade Data Integrity Test Completed ===\n");
    
    TEST_END();
}

/*
 * @Functions:   void test_suite_upgrade_production(void)
 * @Description: 升级流程生产测试套件
 * @Parameters:  NONE
 * @Return:      NONE
 */
void test_suite_upgrade_production(void)
{
    TEST_SUITE_START("Upgrade Production Tests");
    
    test_cd_can_complete_upgrade_flow();
    test_cd_can_upgrade_timeout_scenarios();
    test_cd_can_upgrade_data_integrity();
    
    TEST_SUITE_END("Upgrade Production Tests");
}

// =======================================================================================
// End of file.
// =======================================================================================
